import Vue from 'vue'
import VueRouter from 'vue-router'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    redirect: '/dashboard',
    path: '/home',
    name: 'Homeitem',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: '/articles',
        name: 'article',
        component: () => import('../views/article/article.vue')
      },
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('../views/dashboard/index.vue')
        // component: () => import('@/views/dashboard')
      },
      {
        path: '/companys',
        name: 'companys',
        component: () => import('../views/companys-list/index.vue')
      },
      {
        path: '/directorys',
        component: () => import('../views/subject/directorys.vue'),
        props: true
      },
      {
        path: '/tags',
        component: () => import('../views/subject/tags'),
        props: true
      },
      {
        path: '/list',
        component: () => import('../views/subject/list')
      },
      {
        // 基础试题
        path: '/questions/list',
        name: 'list',
        component: () => import('../views/list/list')
      },
      // 试题录入
      {
        path: '/questions/new',
        name: 'details',
        // component 路径
        // import ( '路径' )为懒加载
        component: () => import('../views/new/new'),
        props: true
      },
      {
        path: '/logs',
        name: 'logs',
        component: () => import(/* webpackChunkName: "about" */ '../views/base/logs.vue')
      },
      {
        path: '/permissions',
        name: 'permissions',
        component: () => import(/* webpackChunkName: "about" */ '../views/base/permissions.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import(/* webpackChunkName: "about" */ '../views/base/users.vue')
      },
      {
        path: '/questions/choice',
        name: 'choice',
        component: () => import(/* webpackChunkName: "about" */ '../views/choice/choice.vue')
      },
      {
        path: '/questions/randoms',
        name: 'check',
        component: () => import(/* webpackChunkName: "about" */ '../views/randoms/randoms.vue')
      },
      {
        path: '/menus',
        name: 'check',
        component: () => import(/* webpackChunkName: "about" */ '../views/base/menus.vue')
      }
    ]
  }
]
const router = new VueRouter({ routes })
// 挂载路由守卫
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要跳转的路径、
  // from 从什么地方跳过来
  // next 是一个函数 放行 next()放行， next('/***')强制跳转的路径
  // const name = localStorage.getItem('name')
  if (to.path === '/login') return next()
  console.log(to)
  // 获取token
  const tokenStr = window.localStorage.getItem('token')
  // 判断是否有token值 如果没有token值 强制跳转登录页面
  if (!tokenStr) return next('/login')
  // 如果有token值  直接跳转
  next()
})

export default router
